//  Created by <Omar MALAGOUEN>

//  Copyright 2011 All rights reserved.
//

/*
 * Various utilities for command line tools
 * Copyright (c) 2000-2010 Omar Malagouen
 *
 * This file is part of Kakola.
 *
 * Kakola is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * Kakola is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with Kakola; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 */

#include <QtGui/QApplication>
#include "kakola.h"



int main(int argc, char *argv[])
{
    QApplication App(argc, argv);

    /*QFile *VERSION_FILE = new QFile("VERSION");
    VERSION_FILE->open(QIODevice::ReadOnly | QIODevice::Text);
    QTextStream flux(VERSION_FILE);
    flux >> VERSION;
    VERSION_FILE->close();*/


    QCoreApplication::setApplicationName("kakola");
    QCoreApplication::setApplicationVersion(VERSION);
    QCoreApplication::setOrganizationName("kakola");
    QCoreApplication::setOrganizationDomain("elfoiros");

    QString bundlePath = QCoreApplication::applicationDirPath() + "/../../" ;

    QString home;
    QString locale = QLocale::system().name();
    CreerLog();
    QFileInfo binary(argv[0]);
    QSettings settings(QSettings::NativeFormat,QSettings::UserScope,QCoreApplication::organizationName(),QCoreApplication::applicationName() );
    QString f = settings.fileName();
    settings.setValue("application",argv[0]);
    QString ressourcesDir = settings.value("ressourcesDir").toString();

    QString srcProfileDir;

    qDebug("Starting %s %s %s", argv[0], argv[1], argv[2]);

#if defined(Q_OS_WIN32) || defined(Q_OS_WIN64)
    ressourcesDir = QFileInfo(argv[0]).absolutePath() ;
#elif defined(Q_OS_MACX)
    ressourcesDir = bundlePath + "/Contents/Resources/documentations/";
    srcProfileDir = bundlePath + "/Contents/Resources/profiles/";
    if ( ! QFile(ressourcesDir).exists())
    {
        qDebug("Bug mac in bundlePath variable : %s not exist",bundlePath.toLatin1().data());
        ressourcesDir = "/Applications/kakola.app/Contents/Resources/documentations/";
    }
#else
    ressourcesDir = "/usr/share/doc/kakola/";
#endif

    settings.setValue("ressourcesDir",ressourcesDir);

    settings.setValue("translateDir",ressourcesDir + "/translate");
    settings.setValue("docDir",ressourcesDir +  "/html/");

    qDebug("Running file %s",argv[0]);
    qDebug("Config file is %s",f.toLatin1().data());

    if ( settings.value("refresh").toString() == "" )
    {
#if defined(Q_OS_WIN32) || defined(Q_OS_WIN64)
        settings.setValue("refresh","10");
#else
        settings.setValue("refresh","50");
#endif
    }

    qDebug("Refresh value for progress bar : %s",settings.value("refresh").toString().toLatin1().data());

#if defined(Q_OS_WIN32) || defined(Q_OS_WIN64)
    QString win32extension = ".exe";
    home=QString(getenv("USERPROFILE"));
#else
    home=QString(getenv("HOME"));
    QString win32extension = "";
#endif

    QStringList listProgram;
    listProgram << "ffmpeg" << "mencoder" << "mplayer"  << "openssl" << "cp" << "ls" << "wget" << "mkvmerge" << "test" << "zip" << "rm" << "unzip" ;
    foreach(QString theProgram, listProgram)
    {
        if (settings.value(theProgram).toString() == "" ||
            ! QFile(settings.value(theProgram).toString()).exists())
        {
            QString newProgram;
            
#if defined(Q_OS_WIN32) || defined(Q_OS_WIN64)
                newProgram = binary.dir().path() + "/" + theProgram + win32extension;
#else
newProgram = theProgram;
#endif
            
            newProgram = QDir::cleanPath(newProgram);
            QFileInfo fileInfoNewProgram(newProgram);
            newProgram = fileInfoNewProgram.canonicalFilePath();
            if (theProgram == "ffmpeg" || theProgram == "mencoder" || theProgram == "mplayer")
            {
                newProgram = "/usr/local/bin/" + theProgram;
            }
            else if ( ! QFile(newProgram).exists())
                newProgram = theProgram;
				qDebug("Writing conf value %s=%s",theProgram.toLatin1().data(),newProgram.toLatin1().data());
            settings.setValue(theProgram, newProgram);
            settings.sync();
        }
    }


    QString profileDir = home + getProfileDir();
    QDir profileQDir(profileDir);

    qDebug("Going to create profile dir %s", profileDir.toLatin1().data());
    if ( profileQDir.mkdir(profileDir) == false && ! profileQDir.exists())
        qDebug("directory %s doest not exist, error while creating directory",profileDir.toLatin1().data());


#if defined(Q_OS_WIN32) || defined(Q_OS_WIN64)
    srcProfileDir= binary.dir().path() + "/profiles/";
#elif defined(Q_OS_MACX)
    srcProfileDir = bundlePath + "/Contents/Resources/profiles/";
#else
    srcProfileDir= "/etc/kakola/profiles/";
#endif

    settings.setValue("srcProfileDir",srcProfileDir);


    QDir srcProfileQDir(srcProfileDir);
    QStringList filters;
    filters << "*.profile"  << "*.png" ;
    srcProfileQDir.setNameFilters(filters);
    QStringList listProfileFile = srcProfileQDir.entryList(QDir::Files | QDir::NoDotAndDotDot);
    qDebug("Import profiles from %s", srcProfileDir.toLatin1().data());
    foreach(QString oneP, listProfileFile){
        QString src = srcProfileDir + oneP;
        QString dst = profileDir;
        dst.replace("\\","/");
        dst = dst + QFileInfo(src).fileName();
        QFile oldFile;
        oldFile.setFileName(dst+".old");
        QFile dstFile;
        dstFile.setFileName(dst);
        if ( ! oldFile.exists() && ! dstFile.exists()){
            if (QFile::copy(src,dst) == true)
                qDebug("Adding profile copy of %s to %s", src.toLatin1().data(), dst.toLatin1().data());
            else
                qDebug("Error copy of %s to %s", src.toLatin1().data(), dst.toLatin1().data());
        }
    }



    QTranslator translator;
    qDebug("Locale = %s",locale.toLatin1().data());

    if (settings.value("language").toString() == "")
    {
        if (locale.contains("fr_FR"))
        {
            qDebug("Langue = Francais");
            settings.setValue("language","French");
        }
        else
        {
            qDebug("Langue = Anglais");
            settings.setValue("language","English");
        }
    }
    QString languageFile = ressourcesDir + "/translate/" +
                           "kakola_" + settings.value("language").toString() + ".qm";
    bool b = translator.load(languageFile);
    if (b == false)
        qDebug("Error while loading language file %s", languageFile.toLatin1().data());
    else
        qDebug("Loading language file %s  ==> OK", languageFile.toLatin1().data());

    qDebug("kakola VERSION is : %s",VERSION);

    App.installTranslator(&translator);
    kakola w;
    w.show();




    return App.exec();
}
